<?php
/**
 * @purpose
 * @Author: Madou
 * @Time  : 2022/8/9 10:26
 */

namespace App\Trip\Orders\Traits;

use Carbon\Carbon;
use Exception;

trait OfflineOrderChangeOrRefundTimeValidTrait
{
    use WorkTimeTrait;

    public bool $is_work_time = true;

    public function hasOtherError(): bool
    {
        return $this->has_other_error;
    }

    public function setIgnoreWorkTimeError(): static
    {
        $this->ignore_worktime_error = true;
        return $this;
    }

    /**
     * 是否是工作时间异常
     *
     * @return bool
     */
    public function isWorkTimeError(): bool
    {
        return !$this->is_work_time;
    }

    /**
     * @throws Exception
     */
    public function throwOtherErrorMsg(string $msg): void
    {
        $this->has_other_error = true;
        $this->throwErrorMessage($msg);
    }

    /**
     * @throws Exception
     * @throws Exception
     */
    public function validTime(): void
    {

        // 如果是线上单
        if ($this->order_info->isOnlineOrder()) {
            $this->is_work_time = true;
            return;
        }


        $time_start = Carbon::today()->addHours($this->workStartTime());
        $time_end   = Carbon::today()->addHours($this->workEndTime());
        if (Carbon::now()->lessThan($time_start) || Carbon::now()->greaterThan($time_end)) {
            $this->throwWorkTimeErrorMsg("线下退改工作时间为：早8：00-晚21：00");
        } else {
            $this->is_work_time = true;
        }
    }

    /**
     * @throws Exception
     */
    public function throwWorkTimeErrorMsg(string $msg): void
    {
        $this->is_work_time = false;
        if ($this->isIgnoreWorkTimeError()) {
            $this->setErrorMsg($msg);
            return;
        }
        $this->throwErrorMessage($msg);
    }

    public function isIgnoreWorkTimeError(): bool
    {
        return $this->ignore_worktime_error;
    }


}